package com.aat.dxfy.http.login;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DisconfDistributedRequest {
	private static Logger log = LoggerFactory.getLogger(DisconfDistributedRequest.class);

	public BasicCookieStore cookieStore = new BasicCookieStore();
	public CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
	private CloseableHttpResponse response;

	private String domain;

	public DisconfDistributedRequest(String domain) {
		this.domain = domain;

		List<Cookie> cookies = cookieStore.getCookies();
		if (cookies.isEmpty()) {
			log.info("None");
		} else {
			for (int i = 0; i < cookies.size(); i++) {
				log.info("- " + cookies.get(i).toString());
			}
		}
	}

	public void login(String name, String passwd) throws IOException {
		//
		HttpPost httpPost = new HttpPost(domain + "/api/account/signin");

		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("name", name));
		nvps.add(new BasicNameValuePair("password", passwd));
		nvps.add(new BasicNameValuePair("remember", "0"));

		httpPost.setEntity(new UrlEncodedFormEntity(nvps));

		response = httpClient.execute(httpPost);

		HttpEntity responseEntity = response.getEntity();

		String res = EntityUtils.toString(responseEntity, "UTF-8");

		log.info("\nlogin:\n\t" + res);

		EntityUtils.consume(responseEntity);

		response.close();

		//
	}

	public void session() throws IOException {
		//
		HttpGet httpGet = new HttpGet(domain + "/api/account/session");

		response = httpClient.execute(httpGet);

		HttpEntity responseEntity = response.getEntity();

		String res = EntityUtils.toString(responseEntity, "UTF-8");

		log.info("\nsession:\n\t" + res);

		EntityUtils.consume(responseEntity);

		response.close();

		//
	}

	public void signout() throws IOException {
		//
		HttpGet httpGet = new HttpGet(domain + "/api/account/signout");

		response = httpClient.execute(httpGet);

		HttpEntity responseEntity = response.getEntity();

		String res = EntityUtils.toString(responseEntity, "UTF-8");

		log.info("\nsignout:\n\t" + res);

		EntityUtils.consume(responseEntity);

		response.close();
		//
	}

	public void close() throws IOException {
		if (response != null) {
			response.close();
		}
		httpClient.close();
	}
}
